package model.workerWithFile;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;

import org.apache.log4j.Logger;

import entities.Garage;
import entities.IGarage;
import entities.IMaster;
import entities.IOrder;
import entities.Master;
import entities.Order;
import entities.OrderStatus;
import model.textFileWorkerWrapper.TextFileWorkerWrapper;

public class ReaderFromFile {

	private ArrayList<IGarage> garages = new ArrayList<IGarage>();
	private ArrayList<IMaster> masters = new ArrayList<IMaster>();
	private ArrayList<IOrder> orders = new ArrayList<IOrder>();

	private static final String PARSER = " ";
	private static final String DELETED = "DELETED";
	private static final String CANCELED = "CANCELED";
	private static final String MADE = "MADE";
	private static final String DATE_FORMAT = "dd.MM.yyyy";

	private static final Logger log = Logger.getLogger(ReaderFromFile.class);

	ArrayList<IMaster> getListMastersFromFile(TextFileWorkerWrapper masterFile) {

		try {
			if (masters.isEmpty()) {
				ArrayList<String> temp = new ArrayList<String>();
				temp = masterFile.readFromFile();
				String arr[] = new String[1];
				for (String s : temp) {
					arr = s.split(PARSER);
					IMaster master = new Master(arr[0]);
					masters.add(master);
				}
			}
		} catch (ExceptionInInitializerError e) {
			log.fatal("File not found");
		} catch (NullPointerException ne) {
			log.fatal("File not found");
		}

		return masters;

	}

	ArrayList<IGarage> getListGaragesFromFile(TextFileWorkerWrapper garageFile) {
		try {
			if (garages.isEmpty()) {
				ArrayList<String> temp = new ArrayList<String>();
				temp = garageFile.readFromFile();
				String arr[] = new String[1];
				for (String s : temp) {
					arr = s.split(PARSER);
					IGarage garage = new Garage(Integer.parseInt(arr[0]));
					garages.add(garage);
				}
			}
		} catch (ExceptionInInitializerError e) {
			log.fatal("File not found");
		} catch (NullPointerException ne) {
			log.fatal("File not found");
		}

		return garages;

	}

	ArrayList<IOrder> getOrderFromFile(TextFileWorkerWrapper orderFile) {
		try {
			if (orders.isEmpty()) {
				ArrayList<String> temp = new ArrayList<String>();
				HashMap<Integer, String> masT = new HashMap<Integer, String>();
				HashMap<Integer, Integer> garT = new HashMap<Integer, Integer>();
				temp = orderFile.readFromFile();
				String arr[] = new String[8];
				for (String s : temp) {
					if (s.equals("")) {
						continue;
					}
					arr = s.split(PARSER);
					IOrder order = new Order(Integer.parseInt(arr[0]),
							parser(arr[1]));
					order.setAllData(parser(arr[2]), parser(arr[3]),
							Integer.parseInt(arr[4]));
					if (arr[5].equals(CANCELED)) {
						order.setOrderStatus(OrderStatus.CANCELED);
					} else if (arr[5].equals(DELETED)) {
						order.setOrderStatus(OrderStatus.DELETED);
					} else if (arr[5].equals(MADE)) {
						order.setOrderStatus(OrderStatus.MADE);
					}
					masT.put(order.getNumberOrder(), arr[6]);
					garT.put(order.getNumberOrder(), Integer.parseInt(arr[7]));

					orders.add(order);
				}

				joinData(masT, garT);
			}
		} catch (ExceptionInInitializerError e) {
			log.fatal("File not found");
		} catch (NullPointerException ne) {
			log.fatal("File not found");
		}
		return orders;

	}

	private GregorianCalendar parser(String h) {
		SimpleDateFormat f = new SimpleDateFormat(DATE_FORMAT);
		GregorianCalendar date1 = new GregorianCalendar();

		try {
			Date date = f.parse(h);
			date1 = (GregorianCalendar) Calendar.getInstance();
			date1.setTime(date);
			f.format(date1.getTime());
		} catch (ParseException pe) {
			log.error("Wrong date format");
		}
		return date1;

	}

	private void joinData(HashMap<Integer, String> masterT,
			HashMap<Integer, Integer> garageT) {

		for (IOrder order : orders) {
			for (IMaster master : masters) {
				for (IGarage garage : garages) {
					if (masterT.get(order.getNumberOrder()).equals(
							master.getNameMaster())
							&& garageT.get(order.getNumberOrder()) == garage
									.getNumberGarage()) {
						order.sendMasterGarage(master, garage);
					}
				}
			}
		}
	}

}
